﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.Interface;
using Cspl.AnimalFight.Lib.EventHandlers;

namespace Cspl.AnimalFight.Lib.Data
{
    public class Duck : AnimalBase, IAttack, IAttackable
    {
        public event AttackDelegate AttackEvent;

        public void Attack(string targetName)
        {
            if (AttackEvent != null)
            {
                // cannot attack if already dead, bug fixed by Havit
                if (this.IsDead)
                {
                    Console.WriteLine("{0} is dead, cannot attack.", this.AnimalName);
                }
                // cannot attack if already escaped, bug fixed by Mario
                else if (this.HasEscaped)
                {
                    Console.WriteLine("{0} has escaped, cannot attack.", this.AnimalName);
                }
                // cannot attack itself bug fixed by Agni
                else if (this._animalName == targetName)
                {
                    Console.WriteLine("Cannot attack itself.");
                }
                else 
                {
                    AttackEventHandlers e = new AttackEventHandlers
                    {
                        AnimalTargetName = targetName,
                        AP = this.AP
                    };

                    AttackEvent(this, e);
                }
            }
        }

        public override void Escape()
        {
            this.HasEscaped = true;
            Console.WriteLine("Duck has escaped.");
        }
    
        public void  Attacked(int ap)
        {
 	                if (this.HasEscaped)
                    {
                        Console.WriteLine("{0} has Escaped, Cannot be Attacked.", this.AnimalName);
                    }
                    else if (this.IsDead)
                    {
                        Console.WriteLine("{0} is alreasy dead. Cannot be attcked.", this.AnimalName);
                    }
                    else
                    {

                        Console.WriteLine("{0} is being attacked. Current HP ={1}.", this.AnimalName, this.HP);
                        this.HP -= (ap - this.DP);
                        Console.WriteLine("Current HP for {0} = {1}", this.AnimalName, this.HP);
                        if (this.HP <= 0)
                        {
                            Console.WriteLine("the {0} is dead", this.AnimalName);
                            this.IsDead = true;
                        }
                    }
        }



    }
}
